<?php

	/*

	//php连接MySQLI
	$link = mysqli_connect('localhost','root','','abc');
		if (!$link){
			printf("不能连接到MySQL. 错误代码: %sn", mysqli_connect_error());
			exit;
		}
	//发送查询指令
			if ($result = mysqli_query($link, 'SELECT * FROM user')){
				//开始获取查询结果
					$data = array();
				while( $row = mysqli_fetch_assoc($result) ){
						array_push($data, $row);
					}
					echo json_encode($data);
						mysqli_free_result($result);//注销结果集。释放内存
			}
				mysqli_close($link); //关闭连接

		$my =  mysql::getIns();
		$str = $my->getAll('SELECT * FROM user');

		echo json_encode($str);
*/


class mysql {
	private static $ins = null;
	private $conn = null;
	private $conf = array();
	private $host ='172.17.0.2'; //主机
	private $user ='root';		//用户名
	private $pwd  ='ADMINadmin';			//用户密码是
	private $db   ='test';		//数据库
    private $port  ='3306';			//用户密码是

    //自动运行构造方法
	protected function __construct(){
		//连接数据库
		$this->connect($this->host,$this->user,$this->pwd,$this->db,$this->port);
		//设置字符集
		$this->charSet('utf8');
	}
	//单例模式
	public static function getIns(){
		if(!(self::$ins instanceof self)){
			self::$ins = new self();
		}
		return self::$ins;
	}
	//负责连接服务器
	public function connect($h,$u,$p,$db,$port){
		$this->conn = mysqli_connect($h,$u,$p,$db,$port);
		if(!$this->conn){
			 exit("帐号和密码错误.数据库连接失败");
		}
		return $this->conn;
	}

	//设置字符集
	protected function charSet($charset){
		$charset = 'set names '.$charset;
		return $this->query($charset);
	}

         //查询多行数据
	public function getAll($sql){
		$rs = $this->query($sql);
		$list = array();
		while($row = mysqli_fetch_assoc($rs)){
			array_push($list, $row);
		}
		return $list;
	}
	//查询单行数据
	public function getRow($sql){
		$rs = $this->query($sql);
		$row = mysqli_fetch_assoc($rs);
		return $row;
	}


	//负责发送sql语句
	public function query($sql){
		$rs = mysqli_query($this->conn,$sql);
		return $rs;
		//执行sql语句写入日志
		//Log::write($sql);
	}


}





function p($arr){
	echo '<pre>';
	print_r($arr);
	echo '</pre>';
}


